const WebSocket = require("ws");
const Mock = require("mockjs");

const wss = new WebSocket.Server({ port: 8000 });

function generateSalesData() {
  return Mock.mock({
    "//1": "今日销售额",
    salesToday: Mock.Random.integer(1000000, 99999999),
    salesLastDay: Mock.Random.integer(1000000, 99999999),
    salesGrowthLastDay: Mock.Random.float(-50, 0, 2, 2),
    salesGrowthLastMonth: Mock.Random.float(0, 100, 2, 2),
  });
}

function generateOrderTodayData() {
  return Mock.mock({
    "//2": "今日订单量",
    orderToday: Mock.Random.integer(1000000, 5000000),
    orderLastDay: Mock.Random.integer(1000000, 5000000),
    orderTrend: Mock.Random.shuffle(
      Array.from({ length: 14 }, () => Mock.Random.integer(100, 1000))
    ),
    orderTrendAxis: [
      "00:00",
      "01:00",
      "02:00",
      "03:00",
      "04:00",
      "05:00",
      "06:00",
      "07:00",
      "08:00",
      "09:00",
      "10:00",
      "11:00",
      "12:00",
      "13:00",
    ],
  });
}

function generateOrderUserData() {
  return Mock.mock({
    "//3": "今日交易用户数",
    orderUser: Mock.Random.integer(100000, 500000),
    returnRate: Mock.Random.float(0, 20, 0, 2),
    orderUserTrend: Mock.Random.shuffle(
      Array.from({ length: 14 }, () => Mock.Random.integer(50, 500))
    ),
    orderUserTrendAxis: [
      "00:00",
      "01:00",
      "02:00",
      "03:00",
      "04:00",
      "05:00",
      "06:00",
      "07:00",
      "08:00",
      "09:00",
      "10:00",
      "11:00",
      "12:00",
      "13:00",
    ],
  });
}

function generateUserTotalData() {
  return Mock.mock({
    "//4": "累计用户数",
    usersTotal: Mock.Random.integer(1000000, 5000000),
    usersLastMonth: Mock.Random.integer(500000, 2000000),
    userGrowthLastDay: Mock.Random.float(0, 30, 0, 2),
    userGrowthLastMonth: Mock.Random.float(0, 100, 0, 2),
  });
}

function generateOrderAndUserData() {
  return Mock.mock({
    "//5": "销售额与访问量",
    orderFullYearAxis: [
      "1月",
      "2月",
      "3月",
      "4月",
      "5月",
      "6月",
      "7月",
      "8月",
      "9月",
      "10月",
      "11月",
      "12月",
    ],
    orderFullYear: Mock.Random.shuffle(
      Array.from({ length: 12 }, () => Mock.Random.integer(100, 1000))
    ),
    userFullYearAxis: [
      "1月",
      "2月",
      "3月",
      "4月",
      "5月",
      "6月",
      "7月",
      "8月",
      "9月",
      "10月",
      "11月",
      "12月",
    ],
    userFullYear: Mock.Random.shuffle(
      Array.from({ length: 12 }, () => Mock.Random.integer(100, 1000))
    ),
  });
}

function generateOrderRankData() {
  return Mock.mock({
    "//6": "排行榜",
    "orderRank|7": [
      {
        "no|+1": 1,
        "name|+1": [
          "肯德基",
          "麦当劳",
          "肯德基",
          "海底捞",
          "西贝筱面村",
          "汉堡王",
          "真功夫",
        ],
        "count|100000-500000": 1,
      },
    ],
  });
}

function generateUserRankData() {
  return Mock.mock({
    "userRank|7": [
      {
        "no|+1": 1,
        "name|+1": [
          "麦当劳",
          "肯德基",
          "必胜客",
          "海底捞",
          "西贝筱面村",
          "汉堡王",
          "真功夫",
        ],
        "count|100000-500000": 1,
      },
    ],
  });
}

function generateSearchData() {
  return Mock.mock({
    "//7": "关键字搜索",
    "searchWord|50": [
      {
        "word|+1": [
          "北京",
          "上海",
          "广州",
          "深圳",
          "南京",
          "杭州",
          "合肥",
          "济南",
          "太原",
          "成都",
          "重庆",
          "苏州",
          "无锡",
          "常州",
          "温州",
          "哈尔滨",
          "长春",
          "大连",
          "沈阳",
          "拉萨",
          "呼和浩特",
          "武汉",
          "南宁",
          "必胜客",
          "肯德基",
          "麦当劳",
          "海底捞",
          "美食",
          "商超",
          "水果",
          "跑腿",
          "送药",
          "烩饭",
          "面条",
          "小龙虾",
          "牛肉",
          "鸡腿",
          "全家桶",
          "麦乐鸡",
          "炭烤",
        ],
        "count|1000-10000": 1,
        "user|100-10000": 1,
      },
    ],
  });
}

function generateSaleRankData() {
  return Mock.mock({
    "//8": "分类销售排行",
    saleRank: {
      category: {
        axisX: [
          "粉面粥店",
          "简餐便当",
          "汉堡披萨",
          "香锅冒菜",
          "小吃炸串",
          "地方菜系",
          "轻食简餐",
        ],
        data: Mock.Random.shuffle(
          Array.from({ length: 7 }, () => Mock.Random.integer(100, 1000))
        ),
      },
      goods: {
        axisX: ["草莓", "甘蔗", "榴莲", "菠萝", "香蕉", "梨", "苹果"],
        data: Mock.Random.shuffle(
          Array.from({ length: 7 }, () => Mock.Random.integer(100, 1000))
        ),
      },
    },
  });
}

// 一次性拿到所有的数据
function getAllDatas() {
  return {
    ...generateSalesData(),
    ...generateOrderTodayData(),
    ...generateOrderUserData(),
    ...generateUserTotalData(),
    ...generateOrderAndUserData(),
    ...generateOrderRankData(),
    ...generateUserRankData(),
    ...generateSearchData(),
    ...generateSaleRankData(),
  };
}

// 将模拟数据生成函数设置成一个列表
const fnList = [
  generateSalesData,
  generateOrderTodayData,
  generateOrderUserData,
  generateUserTotalData,
  generateOrderAndUserData,
  generateOrderRankData,
  generateUserRankData,
  generateSearchData,
  generateSaleRankData,
];

// 创建一个websocket服务
wss.on("connection", (ws) => {
  console.log("Client connected");
  // 利用ws进行数据的send发送，发送一次性请求的数据数据
  ws.send(JSON.stringify(getAllDatas()));

  // 设置一个定时器
  const intervalId = setInterval(() => {
    // 随机下标索引值
    const randomIndex = Math.floor(Math.random() * fnList.length);
    const content = fnList[randomIndex](); // 通过下标索引值对应的函数执行的结果进行获取内容
    const chartUpdateData = {
      type: "update",
      ...content,
    };
    // 将更新的数据发送给客户端
    ws.send(JSON.stringify(chartUpdateData));
  }, 10000);

  ws.on("close", () => {
    clearInterval(intervalId)
    console.log("Client disconnected");
  });
});
